//
//  LGConsultRecordCardiacView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/10/23.
//  心功能等级 - 弹框

#import "LGConsultRecordCardiacView.h"

@interface LGConsultRecordCardiacView ()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIButton *closeBtn;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIButton *sureBtn;

@end

@implementation LGConsultRecordCardiacView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.baseView];
        [self.baseView addSubview:self.titleLabel];
        [self.baseView addSubview:self.closeBtn];
        [self.baseView addSubview:self.tableView];
        [self.baseView addSubview:self.sureBtn];
        [self showCardiacView];
    }
    return self;
}

-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    [self.tableView reloadData];
}

-(void)sureBtnAction{
    if(self.cardiacSelectResult){
        self.cardiacSelectResult(self.content);
    }
    [self hiddenCardiacView];
}

-(void)closeBtnAction{
    [self hiddenCardiacView];
}

-(void)showCardiacView{
    [UIView animateWithDuration:0.3 animations:^{
        self.baseView.frame = CGRectMake(0, Screen_H-viewPix(520)-bottomSafeBarHeight/3.0, Screen_W, viewPix(540)+bottomSafeBarHeight/3.0);
        self.backgroundColor = RGBAColor(0, 0, 0, 0.3);
    }];
}

-(void)hiddenCardiacView{
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor =  RGBAColor(0, 0, 0, 0);
        self.baseView.frame = CGRectMake(0, Screen_H, Screen_W, viewPix(540)+bottomSafeBarHeight/3.0);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGConsultRecordCardiacCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    cell.dataDic = tempDic;
    cell.isSelect = [self.content isEqualToString:tempDic[@"value"]]?YES:NO;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LGConsultRecordCardiacCell *cell = (LGConsultRecordCardiacCell *)[tableView cellForRowAtIndexPath:indexPath];
    if(cell.isSelect == YES){
        self.content = @"";
    }else{
        NSDictionary *tempDic = self.dataArray[indexPath.row];
        self.content = LGNSString(tempDic[@"value"]);
    }
    [self.tableView reloadData];
}


#pragma mark
#pragma mark ====> 创建控件
-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc]initWithFrame:CGRectMake(0, Screen_H, Screen_W, viewPix(540)+bottomSafeBarHeight/3.0)];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = 10;
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"心功能等级" colorString:@"#000000" textFont:LGFontWeight(16, UIFontWeightSemibold) textAlignment:NSTextAlignmentCenter lines:1];
        _titleLabel.frame = CGRectMake(viewPix(60), viewPix(15), Screen_W-viewPix(120), viewPix(40));
    }
    return _titleLabel;
}

-(UIButton *)closeBtn{
    if(!_closeBtn){
        _closeBtn = [UIButton buttonWithImage:@"closeGray16" selectImage:@"closeGray16"];
        _closeBtn.frame = CGRectMake(Screen_W-viewPix(48), viewPix(10), viewPix(40), viewPix(40));
        [_closeBtn addTarget:self action:@selector(closeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, viewPix(50), Screen_W, viewPix(400)) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGConsultRecordCardiacCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
    }
    return _tableView;
}

-(UIButton *)sureBtn{
    if(!_sureBtn){
        _sureBtn = [UIButton buttonWithTitle:@"确定" topMargin:kMaxY(self.tableView.frame)+viewPix(5)];
        [_sureBtn addTarget:self action:@selector(sureBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}

@end



#pragma mark
#pragma mark ====> LGConsultRecordCardiacCell

@interface LGConsultRecordCardiacCell()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *contentLabel;

@end

@implementation LGConsultRecordCardiacCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.baseView];
        [self.baseView addSubview:self.titleLabel];
        [self.baseView addSubview:self.contentLabel];
        [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(6));
            make.left.equalTo(self.contentView).offset(viewPix(16));
            make.right.equalTo(self.contentView).offset(-viewPix(16));
            make.bottom.equalTo(self.contentView).offset(-viewPix(6));
        }];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.equalTo(self.baseView).offset(viewPix(8));
            make.right.equalTo(self.baseView).offset(-viewPix(8));
        }];
        
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).offset(viewPix(10));
            make.left.right.equalTo(self.titleLabel);
            make.bottom.equalTo(self.baseView).offset(-viewPix(10));
        }];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.titleLabel.text = LGNSString(dataDic[@"value"]);
    self.contentLabel.text = LGNSString(dataDic[@"title"]);
}

-(void)setIsSelect:(BOOL)isSelect{
    _isSelect = isSelect;
    if(isSelect == YES){
        self.baseView.layer.borderColor = [UIColor colorWithString:@"#3291FF"].CGColor;
        self.baseView.backgroundColor = [UIColor colorWithString:@"#F6FAFF"];
    }else{
        self.baseView.layer.borderColor = [UIColor colorWithString:@"#D9D9D9"].CGColor;
        self.baseView.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    }
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc]init];
        _baseView.layer.borderWidth = 1;
        _baseView.cornerRidus = 7;
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _titleLabel;
}

-(UILabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _contentLabel;
}
@end
